home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / e / sane80bitFloat.lha / extended_double.e next >
Text File  |  1999-01-10  |  5KB  |  197 lines

  1. -> This module is a raw extract from the MusicIn 'common.c/h'
  2. -> source found on aminet in the archive: MusicIn.lha .
  3. -> It has been ported to AmigaE by Deniil 715!
  4. -> Every character from the C-source was intact when I left it
  5. -> with exception for blanklines and spaces.
  6.  
  7. OPT MODULE
  8.  
  9. /***********************************************************************
  10. *
  11. *  Routines to convert between the Apple SANE extended floating point
  12. *  format and the IEEE double precision floating point format.
  13. *
  14. ***********************************************************************/
  15.  
  16. /*
  17. *** Apple's 80-bit SANE extended has the following format:
  18.  
  19.  1       15      1            63
  20. +-+-------------+-+-----------------------------+
  21. |s|       e     |i|            f                |
  22. +-+-------------+-+-----------------------------+
  23.   msb        lsb   msb                       lsb
  24.  
  25. The value v of the number is determined by these fields as follows:
  26. If 0 <= e < 32767,              then v = (-1)^s * 2^(e-16383) * (i.f).
  27. If e == 32767 and f == 0,       then v = (-1)^s * (infinity), regardless of i.
  28. If e == 32767 and f != 0,       then v is a NaN, regardless of i.
  29.  
  30. *** IEEE Draft Standard 754 Double Precision has the following format:
  31.  
  32. MSB
  33. +-+---------+-----------------------------+
  34. |1| 11 Bits |           52 Bits           |
  35. +-+---------+-----------------------------+
  36.  ^     ^                ^
  37.  |     |                |
  38.  Sign  Exponent         Mantissa
  39. */
  40.  
  41. /* Double and SANE Floating Point Type Definitions */
  42.  
  43. /*
  44. typedef struct  IEEE_DBL_struct {
  45.     unsigned long   hi;
  46.     unsigned long   lo;
  47. } IEEE_DBL;
  48. */
  49.  
  50. EXPORT OBJECT ieee_dbl
  51.  hi:LONG
  52.  lo:LONG
  53. ENDOBJECT
  54.  
  55. /*
  56. typedef struct  SANE_EXT_struct {
  57.     unsigned long   l1;
  58.     unsigned long   l2;
  59.     unsigned short  s1;
  60. } SANE_EXT;
  61. */
  62.  
  63. EXPORT OBJECT sane_ext
  64.  l1:LONG
  65.  l2:LONG
  66.  s1:INT
  67. ENDOBJECT
  68.  
  69. /***********************************************************************
  70. *
  71. *  double_to_extended()
  72. *
  73. *  Purpose:     Convert from IEEE double precision format to SANE
  74. *               extended format.
  75. *
  76. *  Passed:      Pointer to the double precision number and a pointer to
  77. *               what will hold the Apple SANE extended format value.
  78. *
  79. *  Outputs:     The SANE extended format pointer will be filled with
  80. *               the converted value.
  81. *
  82. *  Returned:    Nothing. (E-version returns ext-ptr for convenience)
  83. *
  84. ***********************************************************************/
  85.  
  86. ->void double_to_extended(pd, ps)
  87. ->double *pd;
  88. ->char ps[10];
  89. ->{
  90. EXPORT PROC double_to_extended(p_dbl:PTR TO ieee_dbl,p_ext:PTR TO sane_ext)
  91.  
  92. ->register unsigned long  top2bits;
  93. ->register unsigned short *ps2;
  94. ->register IEEE_DBL       *p_dbl;
  95. ->register SANE_EXT       *p_ext;
  96.  DEF top2bits
  97.  
  98. ->#ifdef  MACINTOSH
  99.  
  100. ->        x96tox80(pd, (extended *) ps);
  101.  
  102. ->#else
  103.  
  104. ->   p_dbl = (IEEE_DBL *) pd;
  105. ->   p_ext = (SANE_EXT *) ps;
  106.  
  107. ->   top2bits = p_dbl->hi & 0xc0000000;
  108.  top2bits:=p_dbl.hi AND $c0000000
  109.  
  110. ->   p_ext->l1 = ((p_dbl->hi >> 4) & 0x3ff0000) | top2bits;
  111.  p_ext.l1:=shr(p_dbl.hi,4) AND $3ff0000 OR top2bits
  112.  
  113. ->   p_ext->l1 |= ((p_dbl->hi >> 5) & 0x7fff) | 0x8000;
  114.  p_ext.l1:=shr(p_dbl.hi,5) AND $7fff OR $8000 OR p_ext.l1
  115.  
  116. ->   p_ext->l2 = (p_dbl->hi << 27) & 0xf8000000;
  117.  p_ext.l2:=Shl(p_dbl.hi,27) AND $f8000000
  118.  
  119. ->   p_ext->l2 |= ((p_dbl->lo >> 5) & 0x07ffffff);
  120.  p_ext.l2:=shr(p_dbl.lo,5) AND $07ffffff OR p_ext.l2
  121.  
  122. ->   ps2 = (unsigned short *) & (p_dbl->lo);
  123. ->   ps2++;
  124. ->   p_ext->s1 = (*ps2 << 11) & 0xf800;
  125.  p_ext.s1:=Shl(p_dbl.lo,11) AND $f800
  126.  
  127. ->#endif
  128.  
  129. ->}
  130. ENDPROC p_ext
  131.  
  132. /***********************************************************************
  133. *
  134. *  extended_to_double()
  135. *
  136. *  Purpose:     Convert from SANE extended format to IEEE double
  137. *               precision format.
  138. *
  139. *  Passed:      Pointer to the Apple SANE extended format value and a
  140. *               pointer to what will hold the IEEE double precision number.
  141. *
  142. *  Outputs:     The IEEE double precision format pointer will be filled
  143. *               with the converted value.
  144. *
  145. *  Returned:    Nothing. (E-version return dbl-ptr for convenience)
  146. *
  147. ***********************************************************************/
  148.  
  149. ->void extended_to_double(ps, pd)
  150. ->char ps[10];
  151. ->double *pd;
  152. ->{
  153. EXPORT PROC extended_to_double(p_ext:PTR TO sane_ext,p_dbl:PTR TO ieee_dbl)
  154.  
  155. ->register unsigned long  top2bits;
  156. ->register IEEE_DBL       *p_dbl;
  157. ->register SANE_EXT       *p_ext;
  158.  DEF top2bits
  159.  
  160. ->#ifdef  MACINTOSH
  161.  
  162. ->   x80tox96((extended *) ps, pd);
  163.  
  164. ->#else
  165.  
  166. ->   p_dbl = (IEEE_DBL *) pd;
  167. ->   p_ext = (SANE_EXT *) ps;
  168.  
  169. ->   top2bits = p_ext->l1 & 0xc0000000;
  170.  top2bits:=p_ext.l1 AND $c0000000
  171.  
  172. ->   p_dbl->hi = ((p_ext->l1 << 4) & 0x3ff00000) | top2bits;
  173.  p_dbl.hi:=Shl(p_ext.l1,4) AND $3ff00000 OR top2bits
  174.  
  175. ->   p_dbl->hi |= (p_ext->l1 << 5) & 0xffff0;
  176.  p_dbl.hi:=Shl(p_ext.l1,5) AND $ffff0 OR p_dbl.hi
  177.  
  178. ->   p_dbl->hi |= (p_ext->l2 >> 27) & 0x1f;
  179.  p_dbl.hi:=shr(p_ext.l2,27) AND $1f OR p_dbl.hi
  180.  
  181. ->   p_dbl->lo = (p_ext->l2 << 5) & 0xffffffe0;
  182.  p_dbl.lo:=Shl(p_ext.l2,5) AND $ffffffe0
  183.  
  184. ->   p_dbl->lo |= (unsigned long) ((p_ext->s1 >> 11) & 0x1f);
  185.  p_dbl.lo:=shr(p_ext.s1,11) AND $1f OR p_dbl.lo
  186.  
  187. ->#endif
  188.  
  189. ->}
  190. ENDPROC p_dbl
  191.  
  192. -> this is for E has no Logic shift right, just arithmetic
  193. PROC shr(x,y)
  194.  MOVE.L x,D0
  195.  MOVE.L y,D1
  196.  LSR.L  D1,D0
  197. ENDPROC D0